
package com.tools.cleanmaster.functionpage;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tools.cleanmaster.R;
import com.tools.cleanmaster.manager.SortProcessManager;
import com.tools.cleanmaster.utils.UiUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppDetailListView extends RelativeLayout {

    class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder> {
        private List<SortProcessManager.ProcessItem> mList;
        private Map<Integer, Boolean> isSelected;
        private View mFooterView;
        public static final int TYPE_FOOTER = 1;
        public static final int TYPE_NORMAL = 2;

        public ListViewAdapter(List<SortProcessManager.ProcessItem> list) {
            mList = list;
            isSelected = new HashMap<Integer, Boolean>();
            for (int i = 0; i < mList.size(); i++) {
                isSelected.put(i, true);
            }
        }

        public void setFooterView(View footerView) {
            mFooterView = footerView;
            notifyItemInserted(getItemCount() - 1);
        }

        public boolean getHasSelected() {
            return isSelected != null && isSelected.containsValue(true);
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            if (mFooterView != null && i == TYPE_FOOTER) {
                return new ViewHolder(mFooterView);
            }
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.function_appsdetail_item, viewGroup, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, final int i) {
            if (getItemViewType(i) == TYPE_NORMAL) {
                if (viewHolder instanceof ViewHolder) {
                    viewHolder.appIcon.setImageDrawable(mList.get(i).icon);
                    viewHolder.appLabel.setText(mList.get(i).label);
                    int value = mList.get(i).percent;
                    if (value > 60) {
                        value = value % 10 + 49;
                    } else if (value < 1) {
                        value = 1;
                    }
                    viewHolder.lineView.setThreshold(threshold);
                    viewHolder.lineView.setCurrentValue(value);
                    if (isBatteryInform) {
                        viewHolder.lineView.setIsBeeLine(false);
                        viewHolder.appPercent.setText(value + "%");
                    } else {
                        viewHolder.lineView.setIsBeeLine(true);
                        if (value > threshold) {
                            viewHolder.appPercent.setText("Lv 3");
                        } else if (value <= 10) {
                            viewHolder.appPercent.setText("Lv 1");
                        } else {
                            viewHolder.appPercent.setText("Lv 2");
                        }
                    }
                    viewHolder.appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            isSelected.put(i, isChecked);
                        }
                    });
                    if (isSelected.containsKey(i) && isSelected.get(i)) {
                        viewHolder.appCheck.setChecked(true);
                    } else if (!isSelected.containsKey(i)) {
                        viewHolder.appCheck.setChecked(true);
                    } else {
                        viewHolder.appCheck.setChecked(false);
                    }
                    return;
                }
                return;
            } else {
                return;
            }

        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            if (mFooterView == null) {
                return mList.size();
            } else {
                return mList.size() + 1;
            }
        }

        @Override
        public int getItemViewType(int position) {
            if (mFooterView == null) {
                return TYPE_NORMAL;
            }
            if (position == getItemCount() - 1) {
                return TYPE_FOOTER;
            }
            return TYPE_NORMAL;
        }

        public void clear() {
            mList.clear();
            notifyDataSetChanged();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView appIcon;
            public TextView appLabel;
            public TextView appPercent;
            public CheckBox appCheck;
            public SchematicLineView lineView;

            public ViewHolder(View view) {
                super(view);
                if (view == mFooterView) {
                    return;
                }
                appIcon = view.findViewById(R.id.app_icon);
                appLabel = view.findViewById(R.id.app_label);
                appPercent = view.findViewById(R.id.app_percent);
                appCheck =  view.findViewById(R.id.app_checked);
                lineView = view.findViewById(R.id.app_line);
            }
        }
    }

    private RecyclerView mRecyclerView;
    private ListViewAdapter mAdapter;
    private Animation mFlipAnimation;
    private LayoutAnimationController mFlipAnimationController;
    private Context mContext;
    private List<SortProcessManager.ProcessItem> mProcessItems;
    private boolean isBatteryInform = false;
    private int threshold = 50;

    public AppDetailListView(Context ctx) {
        super(ctx);
        init();
    }

    public AppDetailListView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        init();
    }

    private void init() {
        mContext = getContext();
        inflate(getContext(), R.layout.function_apps_detail, this);
        mFlipAnimation = AnimationUtils.loadAnimation(mContext, R.anim.appicon_out_anim);
        mFlipAnimation.setFillAfter(true);
        mFlipAnimationController = new LayoutAnimationController(mFlipAnimation);
        mFlipAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
        mFlipAnimationController.setDelay(0.4f);
    }

    @Override
    public void onFinishInflate() {
        super.onFinishInflate();
        mRecyclerView = findViewById(R.id.appicon_recycler);
    }

    public void clearAppAnimation(final Animation.AnimationListener listener) {
        mRecyclerView.setLayoutAnimation(mFlipAnimationController);
        mRecyclerView.setLayoutAnimationListener(listener);
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.notifyDataSetChanged();
                mRecyclerView.startLayoutAnimation();
            }
        }, 50);
    }

    public void setAppsIcons(List<SortProcessManager.ProcessItem> processItems) {
        mProcessItems = processItems;
        mAdapter = new ListViewAdapter(mProcessItems);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, 1, false));
        mRecyclerView.setHasFixedSize(true);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<String, Integer>();
        stringIntegerHashMap.put(SpaceItemDecoration.TOP_DECORATION, 11);
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(stringIntegerHashMap));
        TextView footerView = new TextView(mContext);
        footerView.setBackgroundColor(Color.YELLOW);
        footerView.setHeight(UiUtils.dipToPx(mContext, 60));
        mAdapter.setFooterView(footerView);
        mRecyclerView.setAdapter(mAdapter);
    }

    public void setIsBatteryInform(boolean isBatteryInform, int threshold) {
        this.isBatteryInform = isBatteryInform;
        this.threshold = threshold;
        mAdapter.notifyDataSetChanged();
    }

    public boolean getHasSelected() {
        if (mAdapter == null) {
            return true;
        }
        return mAdapter.getHasSelected();
    }
}
